<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="订单号" prop="orderSn">
        <el-input v-model="formData.orderSn" placeholder="请输入订单号" />
      </el-form-item>
       <el-form-item label="订单状态" prop="orderSn">
        <el-select v-model="formData.status" placeholder="请选择订单状态">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        <!-- <el-input v-model="formData.orderSn" placeholder="请输入订单号" /> -->
      </el-form-item>
      <el-form-item label="商砼企业" prop="companyId">
        <el-select v-model="formData.companyId" placeholder="请选择商砼企业" clearable class="!w-240px">
          <el-option :label="item.nickname" :value="item.id" v-for="(item) in componyList" :key="item.id"/>
        </el-select>
        <!-- <el-input v-model="formData.companyId" placeholder="请输入商砼企业ID" /> -->
      </el-form-item>
      <el-form-item label="项目" prop="projectId">
        <el-select v-model="formData.projectId" placeholder="请选择项目" clearable class="!w-240px">
          <el-option :label="item.company" :value="item.id" v-for="(item) in projectList" :key="item.id"/>
        </el-select>
        <!-- <el-input v-model="formData.projectId" placeholder="请输入项目id" /> -->
      </el-form-item>
      <el-form-item label="采购需求id" prop="purchaseId">
        <el-select v-model="formData.purchaseId" placeholder="请选择采购需求" clearable class="!w-240px">
          <el-option :label="item.id" :value="item.id" v-for="(item) in purchaseList" :key="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="商品" prop="goodsId">
        <el-select v-model="formData.goodsId" placeholder="请选择商品" clearable class="!w-240px">
          <el-option :label="item.name" :value="item.id" v-for="(item) in goodsList" :key="item.id"/>
        </el-select>
        <!-- <el-input v-model="formData.goodsId" placeholder="请输入商品id" /> -->
      </el-form-item>
      <el-form-item label="需要数量" prop="needNumber">
        <el-input v-model="formData.needNumber" placeholder="请输入需要数量" />
      </el-form-item>
      <el-form-item label="本次数量" prop="number">
        <el-input v-model="formData.number" placeholder="请输入本次数量" />
      </el-form-item>
      <el-form-item label="已有数量" prop="endNumber">
        <el-input v-model="formData.endNumber" placeholder="请输入已有数量" />
      </el-form-item>
      <el-form-item label="到达照片">
        <UploadImg v-model="formData.arrivalPics" />
      </el-form-item>
      <el-form-item label="签收照片">
        <UploadImg v-model="formData.signPics" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import * as PurchaseLogApi from '@/api/system/purchaseLog'
import * as UserApi from '@/api/system/user'
import * as ProjectApi from '@/api/system/project'
import * as PurchaseApi from '@/api/system/purchase'
import * as GoodsApi from '@/api/cms/goods'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const componyList = ref([]) // 企业列表
const purchaseList = ref([]) // 采购需求列表
const goodsList = ref([]) // 商品列表
const projectList = ref([]) // 项目列表
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const options = ref([
 {
   value:0,
  label:'待配送'
 },
 {
   value:1,
  label:'待签收'
 },
 {
   value:2,
  label:'已签收'
 },
])
const formData = ref({
  id: undefined,
  orderSn: undefined,
  companyId: undefined,
  projectId: undefined,
  purchaseId: undefined,
  goodsId: undefined,
  needNumber: undefined,
  number: undefined,
  endNumber: undefined,
  arrivalPics: undefined,
  signPics: undefined,
  status:undefined
})
const formRules = reactive({
  orderSn: [{ required: true, message: '订单号不能为空', trigger: 'blur' }],
  companyId: [{ required: true, message: '商砼企业ID不能为空', trigger: 'blur' }],
  projectId: [{ required: true, message: '项目id不能为空', trigger: 'blur' }],
  purchaseId: [{ required: true, message: '采购需求id不能为空', trigger: 'blur' }],
  goodsId: [{ required: true, message: '商品id不能为空', trigger: 'blur' }],
  needNumber: [{ required: true, message: '需要数量不能为空', trigger: 'blur' }],
  number: [{ required: true, message: '本次数量不能为空', trigger: 'blur' }],
  endNumber: [{ required: true, message: '已有数量不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  // 加载企业列表
  componyList.value = await UserApi.getSimpleCompanyList()
  let res  = await ProjectApi.getProjectPage({pageNo:1,pageSize:100})
  projectList.value = res.list
  let res2  = await PurchaseApi.getPurchasePage({pageNo:1,pageSize:100})
  const data = await GoodsApi.getGoodsPage({pageNo:1,pageSize:100})
  goodsList.value = data.list
  console.log(res2,'ress222222')
  purchaseList.value = res2.list
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await PurchaseLogApi.getPurchaseLog(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  console.log(formData,'formData')
  // return
  try {
    const data = formData.value as unknown as PurchaseLogApi.PurchaseLogVO
    if (formType.value === 'create') {
      await PurchaseLogApi.createPurchaseLog(data)
      message.success(t('common.createSuccess'))
    } else {
      await PurchaseLogApi.updatePurchaseLog(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    orderSn: undefined,
    companyId: undefined,
    projectId: undefined,
    purchaseId: undefined,
    goodsId: undefined,
    needNumber: undefined,
    number: undefined,
    endNumber: undefined,
    arrivalPics: undefined,
    signPics: undefined
  }
  formRef.value?.resetFields()
}
</script>
